import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from 'unplugin-vue-components/resolvers';
import legacy from '@vitejs/plugin-legacy';
import { VitePWA } from 'vite-plugin-pwa';
import { viteMockServe } from 'vite-plugin-mock';
import AutoImport from 'unplugin-auto-import/vite';
import path from 'path';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env: any = loadEnv(mode, 'env');
  const sevHost: any = env.VITE_SERVER_HOST;
  const sevPort: any = env.VITE_SERVER_PORT;
  return {
    plugins: [
      vue({
        reactivityTransform: true
      }),
      Components({
        resolvers: [VantResolver()]
      }),
      vueJsx(),
      legacy(),
      VitePWA(),
      viteMockServe({
        mockPath: 'mock',
        localEnabled: true
      }),
      AutoImport({
        imports: ['vue', 'vue-router']
      })
    ],
    envDir: 'env',
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
        '@c': path.resolve(__dirname, 'src/components'),
        '@u': path.resolve(__dirname, 'src/utils'),
        '@v': path.resolve(__dirname, 'src/views'),
        '@s': path.resolve(__dirname, 'src/store'),
        '@o': path.resolve(__dirname, 'src/ocean')
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/style/theme.scss";'
        }
      }
    },
    server: {
      host: sevHost,
      port: sevPort,
      proxy: {
        '/dev/mock': {
          target: 'http://172.0.0.1:3001/',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/dev/, '')
        },
        '/dev': {
          target: 'http://192.168.1.102:1000/',
          // target: 'http://192.168.0.110:1000/',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/dev/, '')
        }
        // '/socket.io': {
        //   target: 'http://192.168.0.110:1000/',
        //   ws: true,
        //   changeOrigin: true
        // }
      }
    },
    // build: {
    //   chunkSizeWarningLimit: 5000,
    //   maximumFileSizeToCacheInBytes: 10000
    // }
  
  };
});
